package com.nanyang.tms.service.company.impl;

import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ExceptionProcessorUtils;
import com.nanyang.common.core.exception.ServiceException;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.tms.api.dto.request.companydeclare.CompanyDeclareAddRequest;
import com.nanyang.tms.api.dto.request.companydeclare.CompanyDeclareDelRequest;
import com.nanyang.tms.api.dto.request.companydeclare.CompanyDeclareQueryRequest;
import com.nanyang.tms.api.dto.request.companydeclare.CompanyDeclareUpdateRequest;
import com.nanyang.tms.api.dto.response.companydeclare.CompanyDeclarePageResponse;
import com.nanyang.tms.api.dto.response.companydeclare.CompanyDeclareResponse;
import com.nanyang.tms.api.service.companydeclare.CompanyDeclareRpcService;
import com.nanyang.tms.dao.company.CompanyDeclareDao;
import com.nanyang.tms.domain.company.CompanyDeclareDO;
import com.nanyang.tms.domain.company.CompanyDeclareQuery;
import com.nanyang.tms.domain.company.CompanyDeclareVO;
import com.nanyang.tms.manager.company.CompanyDeclareManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.Objects;

@Slf4j
@Service(version = "1.0.0", timeout = 10000, retries = 0, validation = "true")
public class CompanyDeclareRpcServiceImpl implements CompanyDeclareRpcService {


    @Autowired
    private CompanyDeclareManager companyDeclareManager;
    @Autowired
    private CompanyDeclareDao companyDeclareDao;
    @Override
    public ApiResult<PageBean<CompanyDeclarePageResponse>> listCompanyDeclare(CompanyDeclareQueryRequest param) {
        ApiResult<PageBean<CompanyDeclarePageResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            CompanyDeclareQuery query = new CompanyDeclareQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<CompanyDeclareVO> companyDeclareList = companyDeclareManager.listCompanyDeclare(query);
            PageBean<CompanyDeclarePageResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(companyDeclareList.getTotalItem());
            BeanUtils.copyListProperties(companyDeclareList.getData(), resultList.getData(), CompanyDeclarePageResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }
    @Override
    public ApiResult<CompanyDeclareResponse> selectByCode(CompanyDeclareQueryRequest param) {
        ApiResult<CompanyDeclareResponse> result = new ApiResult<>();
        try {
            CompanyDeclareResponse  companyDeclareResult = new CompanyDeclareResponse();
            CompanyDeclareQuery query=new CompanyDeclareQuery();
            BeanUtils.copyProperties(param, query);
            CompanyDeclareVO companyDeclareVo = companyDeclareDao.selectByCode(query);
            if(Objects.isNull(companyDeclareVo)){
                throw new ServiceException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
            }
            BeanUtils.copyProperties(companyDeclareVo,  companyDeclareResult);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData( companyDeclareResult);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<Boolean> saveCompanyDeclare(CompanyDeclareAddRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            CompanyDeclareDO companyDeclareDO = new  CompanyDeclareDO();
            BeanUtils.copyProperties(param,companyDeclareDO);
            companyDeclareDO.setGmtCreatePin(param.getPin());
            companyDeclareManager.saveCompanyDeclare(companyDeclareDO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<Boolean> updateCompanyDeclare(CompanyDeclareUpdateRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            CompanyDeclareDO companyDeclareDO = new  CompanyDeclareDO();
            BeanUtils.copyProperties(param,companyDeclareDO);
            companyDeclareDO.setGmtModifiedPin(param.getPin());
            companyDeclareDao.update(companyDeclareDO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<Boolean> delCompanyDeclare(CompanyDeclareDelRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            CompanyDeclareDO companyDeclareDO=new CompanyDeclareDO();
            BeanUtils.copyProperties(param,companyDeclareDO);
            companyDeclareDO.setGmtModifiedPin(param.getPin());
            companyDeclareDao.delDeclare(companyDeclareDO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
}
